package ua.dogar.threads;

import ua.dogar.vectors.Vector;

public class Keeper {
	private Vector vec;
	private boolean flag = false;
	
	public Keeper(Vector vec) {
		this.vec = vec;
	}
	synchronized public void setData()
		      throws InterruptedException {
		 
		 for(int i =0;i<vec.getLength();i++){
			 while(flag)
		            wait();
				double genValue = Math.round(Math.random()*100);
				vec.setElement(genValue, i);
				System.out.println("Write:"+genValue+" to position "+i);
				flag = true;
				notifyAll();
			}
	      
		    }
	synchronized public void getData(){
		 try {
	           
	           for (int i = 0; i < vec.getLength(); i++) {
	        	   while(!flag)
		                wait();
	    			System.out.println("Read:" + vec.getElement(i) + " from position " + i);
	    			 flag = false;
	 	            notifyAll();
	    		}
	        } catch(InterruptedException e) {
	            Thread.currentThread().interrupt();
	        }


	}

}
